{extend name="normal_base" /}
{block name="navigation"}{/block}
{block name="css"}
<style>
</style>
{/block}
{block name="body"}

<div class="x-body">
    <blockquote class="layui-elem-quote" style="color: #AAAAAA">Tip ： 对于同样筛选条件的记录，直接下载即可，不用再重复打包。如果一定要重新打包，请先删除掉同样条件的打包记录。</blockquote>

    <form class="layui-form" id="search-form-id" method="GET" action="{:url('pack_download_record')}">
        <div class="layui-input-inline">
            <select name="project_group_id" id="project_group_id" lay-filter="sign_company_code">
                <option value=0>项目组选择</option>
                {foreach name='project_group_list' item='v' key="k"}
                <option value="{$k}" {if condition="$k==$project_group_id"}selected="selected"{/if}>{$v}</option>
                {/foreach}
            </select>
        </div>

        <div class="layui-input-inline">
            <input class="layui-input" placeholder="回收开始时间" name="recycle_begin" value="{$recycle_begin}" id="recycle_begin">
        </div>

        <div class="layui-input-inline">
            <input class="layui-input" placeholder="截止时间" name="recycle_end" value="{$recycle_end}" id="recycle_end">
        </div>
    </form>
    <br>
    <xblock>
        <button class="layui-btn"  lay-submit="" lay-filter="sreach" id="sreach-by-where">搜索</button>
        <button type="reset" class="layui-btn layui-btn-normal" id="reset-search">重置</button>
        <button class="layui-btn" id="pack-by-where">扫描件打包</button>
        <button class="layui-btn layui-btn-normal" onclick="javascript:location.replace(location.href);">页面刷新</button>
    </xblock>


    <table class="layui-table" lay-size="">
        <thead>
        <tr>
            <th>项目组</th>
            <th>回收起始时间</th>
            <th>回收截止时间</th>
            <th>结果</th>
            <th>打包时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {foreach name="list" item="vo"}
        <tr class="customer-tr" data-id='{$vo.id}'>
            <td>{if condition="isset($project_group_list[$vo.project_group_id])"}{$project_group_list[$vo.project_group_id]}{/if}</td>
            <td>{$vo.recycle_begin}</td>
            <td>{$vo.recycle_end}</td>
            <td>{$vo.status_desc}</td>
            <td>{$vo.create_time}</td>
            <td class="td-manage">
                {if condition="empty($vo.down_url) == false"}
                <a title="下载打包文件" onclick="download_scanning_file(this, '{$vo.down_url}')" href="javascript:;">
                    <i class="fa fa-download"></i>
                </a> &nbsp;
                {/if}

                <a title="删除" onclick="delOne(this,{$vo.id})" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
            </td>
        </tr>
        {/foreach}
        </tbody>
    </table>
    <!--分页-->
    {$list->render()}
</div>

{/block}

{block name="script"}
<script src="__JS__/jquery.fileDownload.js"></script>
<script>
    layui.use(['form','layer','laydate'], function(){

        $ = layui.jquery;
        var form = layui.form
                ,layer = layui.layer;

        var laydate = layui.laydate;

        laydate.render({
            elem: '#recycle_begin' //指定元素
            ,type: 'date'
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#recycle_end' //指定元素
            ,type: 'date'
        });
    });

    /*下载扫描件*/
    function download_scanning_file(obj, file_url) {
        var url = '{:url("down_scnning_file")}';
        var layer = layui.layer;
        $.fileDownload(url,{
            httpMethod: 'GET',
            data:{file_url:file_url},
            prepareCallback:function(url){
                //layer.load();
            },
            successCallback:function(url){
                /*layer.closeAll('loading');
                layer.msg('下载成功', {icon:6, time:1500});*/
            },
            failCallback: function (html, url) {
                //layer.closeAll('loading');
                layer.msg(html, {icon:5, time:1500});
            }
        });
    }

    /*按钮绑定提交表单*/
    $('#sreach-by-where').bind('click', function () {
        $("#search-form-id").submit();
    });

    /*绑定打包按钮*/
    $('#pack-by-where').bind('click', function () {
        var layer = layui.layer;
        //检查是否选择了项目组
        var project_group_id = $('#project_group_id').val();
        if (project_group_id <= 0) {
            layer.msg('请选择项目组！', {icon:5,time:1500});
            return;
        }
        var emReg = /^\d{4}-\d{2}-\d{2}$/;
        var recycle_begin = $('#recycle_begin').val();
        if (recycle_begin == '') {
            layer.msg("请输入起始时间", {icon:5,time:1500});
            return;
        }
        if (!emReg.test(recycle_begin)) {
            layer.msg("请输入正确的时间格式，比如：2018-01-01!", {icon:5,time:1500});
            return;
        }
        var recycle_end = $('#recycle_end').val();
        if (recycle_end == '') {
            layer.msg("请输入截止时间", {icon:5,time:1500});
            return;
        }
        if (!emReg.test(recycle_end)) {
            layer.msg("请输入正确的时间格式，比如：2018-01-01!", {icon:5,time:1500});
            return;
        }

        layer.msg("打包请求已提交，请等待结果...", {icon:6,time:1500});
        /*此时禁用打包按钮，防止重复提交*/
        $("#pack-by-where").attr({"class":"layui-btn layui-btn-disabled"});

        /*3秒后恢复打包按钮*/
        setTimeout(function () {
            $("#pack-by-where").attr({"class":"layui-btn"});
        }, 3000);

        /*发起打包申请*/
        $.ajax({
            type: "post",
            url:"{:url('pack_scanning_file')}",
            data:{project_group_id:project_group_id,recycle_begin:recycle_begin,recycle_end:recycle_end},
            dataType: "json",
            success: function(data){
                if (data.code == 1) {
                    layer.msg(data.msg,{icon:5,time:1500});
                } else {
                    var is_old = data.msg.old;
                    var file_url = data.msg.url;
                    layer.open({
                        title:'通知',
                        type: 1
                        ,skin:'layui-layer-molv'
                        ,offset: 'rb' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                        ,area: ['200px']
                        ,id: 'pack-success' //防止重复弹出
                        ,content: '<div style="padding: 20px;">打包完成，<a href="'+file_url+'"><span style="color: #FC754B">请点击下载</span></a></div>'
                        /*,btn: '关闭'
                        ,btnAlign: 'c' //按钮居中*/
                        ,shade: 0 //不显示遮罩
                        ,yes: function(){
                            layer.closeAll();
                        }
                    });
                }
            }, error: function(){
                layer.msg("请求出错",{icon:5,time:1500});
            }
        });

    });

    /*重置按钮*/
    $('#reset-search').bind('click', function () {
        $('#project_group_id').val(0);
        $('#recycle_begin').val('');
        $('#recycle_end').val('');
        layui.form.render('select');
    });

    /*删除*/
    function delOne(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            //发异步删除数据
            $.ajax({
                type: "post",
                url: "delete_pack_record?id="+id,
                dataType: "json",
                success: function(data){
                    if (data.code == 1) {
                        layer.msg(data.msg);
                    } else {
                        layer.msg('已删除!',{icon:1,time:1000});
                        $(obj).parents("tr").remove();
                    }
                }, error: function(){
                    layer.msg("请求出错",{icon:5,time:1000});
                }
            });
        });
    }
</script>
{/block}